/*

Boost Software License - Version 1.0 - August 17th, 2003

Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license ( the "Software" ) to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:

The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

*/
module derelict.opengl3.wgl;

version( Windows ) {

    private {
        import std.string;
        import derelict.util.wintypes;
    }

    extern( Windows ) nothrow {
        alias da_wglCopyContext = BOOL function( void*,void* );
        alias da_wglCreateContext = void* function( void* );
        alias da_wglCreateLayerContext = void* function( void*,int );
        alias da_wglDeleteContext = BOOL function( void* );
        alias da_wglDescribeLayerPlane = BOOL function( void*,int,int,UINT,LAYERPLANEDESCRIPTOR* );
        alias da_wglGetCurrentContext = void* function();
        alias da_wglGetCurrentDC = void* function();
        alias da_wglGetLayerPaletteEntries = int function( void*,int,int,int,COLORREF* );
        alias da_wglGetProcAddress = FARPROC function( LPCSTR );
        alias da_wglMakeCurrent = BOOL function( void*,void* );
        alias da_wglRealizeLayerPalette = BOOL function( void*,int,BOOL );
        alias da_wglSetLayerPaletteEntries = int function( void*,int,int,int,COLORREF* );
        alias da_wglShareLists = BOOL function( void*,void* );
        alias da_wglSwapLayerBuffers = BOOL function( void*,UINT );
        alias da_wglUseFontBitmapsW = BOOL function( void*,DWORD,DWORD,DWORD );
        alias da_wglUseFontOutlinesW = BOOL function( void*,DWORD,DWORD,DWORD,FLOAT,FLOAT,int,GLYPHMETRICSFLOAT* );

    }

    __gshared {
        da_wglCopyContext wglCopyContext;
        da_wglCreateContext wglCreateContext;
        da_wglCreateLayerContext wglCreateLayerContext;
        da_wglDeleteContext wglDeleteContext;
        da_wglDescribeLayerPlane wglDescribeLayerPlane;
        da_wglGetCurrentContext wglGetCurrentContext;
        da_wglGetCurrentDC wglGetCurrentDC;
        da_wglGetLayerPaletteEntries wglGetLayerPaletteEntries;
        da_wglGetProcAddress wglGetProcAddress;
        da_wglMakeCurrent wglMakeCurrent;
        da_wglRealizeLayerPalette wglRealizeLayerPalette;
        da_wglSetLayerPaletteEntries wglSetLayerPaletteEntries;
        da_wglShareLists wglShareLists;
        da_wglSwapLayerBuffers wglSwapLayerBuffers;
        da_wglUseFontBitmapsW wglUseFontBitmapsW;
        da_wglUseFontOutlinesW wglUseFontOutlinesW;
    }

    alias wglUseFontBitmaps = wglUseFontBitmapsW;
    alias wglUseFontOutlines = wglUseFontOutlinesW;

    package {
        void loadPlatformGL( void delegate( void**, string, bool doThrow ) bindFunc ) {
            bindFunc( cast( void** )&wglCopyContext, "wglCopyContext", true );
            bindFunc( cast( void** )&wglCreateContext, "wglCreateContext", true );
            bindFunc( cast( void** )&wglCreateLayerContext, "wglCreateLayerContext", true );
            bindFunc( cast( void** )&wglDeleteContext, "wglDeleteContext", true );
            bindFunc( cast( void** )&wglDescribeLayerPlane, "wglDescribeLayerPlane", true );
            bindFunc( cast( void** )&wglGetCurrentContext, "wglGetCurrentContext", true );
            bindFunc( cast( void** )&wglGetCurrentDC, "wglGetCurrentDC", true );
            bindFunc( cast( void** )&wglGetLayerPaletteEntries, "wglGetLayerPaletteEntries", true );
            bindFunc( cast( void** )&wglGetProcAddress, "wglGetProcAddress", true );
            bindFunc( cast( void** )&wglMakeCurrent, "wglMakeCurrent", true );
            bindFunc( cast( void** )&wglRealizeLayerPalette, "wglRealizeLayerPalette", true );
            bindFunc( cast( void** )&wglSetLayerPaletteEntries, "wglSetLayerPaletteEntries", true );
            bindFunc( cast( void** )&wglShareLists, "wglShareLists", true );
            bindFunc( cast( void** )&wglSwapLayerBuffers, "wglSwapLayerBuffers", true );
            bindFunc( cast( void** )&wglUseFontBitmapsW, "wglUseFontBitmapsW", true );
            bindFunc( cast( void** )&wglUseFontOutlinesW, "wglUseFontOutlinesW", true );
        }

        void* loadGLFunc( string symName ) {
            return cast( void* )wglGetProcAddress( symName.toStringz() );
        }

        bool hasValidContext() {
            if( wglGetCurrentContext && wglGetCurrentContext() )
                return true;
            return false;
        }
    }
}
